import md5 from 'js-md5'

export const stringTime = function (now) {
  var year = now.getFullYear()
  var month = now.getMonth() + 1
  var day = now.getDate()
  var h = now.getHours()
  var m = now.getMinutes()
  var s = now.getSeconds()
  month = month < 10 ? '0' + month : month
  day = day < 10 ? '0' + day : day
  h = h < 10 ? '0' + h : h
  m = m < 10 ? '0' + m : m
  s = s < 10 ? '0' + s : s
  var str = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s
  return str
}

export const dataForm2 = function (data) {
  var str = ''
  for (var key in data) {
    if (Object.prototype.hasOwnProperty.call(data, key)) {
      if (data[key]) {
        if (key !== 'image') {
          str += key + '=' + data[key] + '&'
        }
      }
    }
  }
  str = str.substring(0, str.length - 1)
  return str
}

export const dataForm = function (data) {
  var str = ''
  for (var key in data) {
    if (Object.prototype.hasOwnProperty.call(data, key)) {
      if (data[key]) {
        if (key === 'image') {
          continue
        }
        str += key + '=' + data[key] + '&'
      }
    }
  }
  str = str.substring(0, str.length - 1)
  return str
}

export const requestData = function (data) {
  var sign = dataForm(data)
  sign = md5(sign).toUpperCase()
  data.sign = sign
  var str = dataForm(data)
  return str
}

export const requestData2 = function (data) {
  var sign = dataForm2(data)
  sign = md5(sign).toUpperCase()
  data.sign = sign
  var str = dataForm(data)
  return str
}

export const rand = function (x, y) {
  return Math.floor(Math.random() * (y - x + 1) + x)
}
